/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Daniel Uppenkamp
 */
public class Player {

    /**
     * The player number.
     */
    protected int playerNumber;
    /**
     * The player's cards.
     */
    protected List<Card> cards = new ArrayList();
    /**
     * The player's marbles.
     */
    protected List<Marble> marbles = new ArrayList();

    /**
     * Creates a new Player.
     *
     * @param playerNumber the player number
     */
    public Player(int playerNumber) {

        this.playerNumber = playerNumber;

        // create the marbles for this player
        for (int i = 0; i < GameLogic.NUMBER_OF_MARBLES_PER_PLAYER; i++) {
            marbles.add(new Marble(playerNumber, i, Board.BOARD_INDEX_START_MIN[playerNumber] + i));
        }

    }

    /**
     * Creates a new Player.
     *
     * @param player the player to copy
     */
    public Player(Player player) {

        Preconditions.checkNotNull(player, "Null player");

        this.playerNumber = player.playerNumber;

        // create the marbles to match the given player's marbles
        for (Marble marble : player.getMarbles()) {
            marbles.add(new Marble(playerNumber, marble.getMarbleNumber(), marble.getBoardIndex()));
        }

        // copy the cards
        this.cards.addAll(player.getCards());

    }

    /**
     * Returns the player's cards.
     *
     * @return the player's cards
     */
    public List<Card> getCards() {
        return cards;
    }

    /**
     * Returns the player's marble with the given marble number.
     *
     * @param marbleNumber the marble number
     * @return the player's marble with the given marble number
     */
    public Marble getMarble(int marbleNumber) {
        Preconditions.checkArgument(marbleNumber >= 0 && marbleNumber < marbles.size(), "Invalid marble number");
        return marbles.get(marbleNumber);
    }

    /**
     * Returns the player's marbles.
     *
     * @return the player's marbles
     */
    public List<Marble> getMarbles() {
        return marbles;
    }

    /**
     * Returns the player number.
     *
     * @return the player number
     */
    public int getPlayerNumber() {
        return playerNumber;
    }

}
